<!-- 操作角色弹框 -->
<template>
    <dy-dialog :title="title" ref="dialog" width="35%" @submitHandle="submitHandle">
        <el-form :model="formData" label-width="100px" ref="form" v-loading="loading" size="default">
            <el-form-item label="角色名称" required>
                <el-input v-model="formData.role_name" />
            </el-form-item>
            <el-form-item label="描述">
                <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="formData.remark" />
            </el-form-item>
        </el-form>
    </dy-dialog>
</template>

<script setup>
import { reactive, ref, toRefs } from 'vue'
import api from '@/api'
import { ElMessage } from 'element-plus'

let emits = defineEmits(['cbhandle'])

let state = reactive({
    loading: false,
    title: ''
})
let {
    loading, title
} = toRefs(state)

let formData = reactive({})

let form = ref(null)
let dialog = ref(null)

//打开弹框执行
let openFunction = (params) => {
    state.title = params.type == 'add' ? '新增角色' : '编辑角色'
    if(params.type == 'edit'){
        formData = params.roleRow 
    }
    formData.departmentId = params.departmentId
}

//提交
let submitHandle = async () => {
    if(!formData.role_name){
        ElMessage({
            message: '请完善角色信息',
            type: 'warning'
        })
        return
    }
    let params = {}
    params.role_name = formData.role_name
    params.description = formData.description
    params.remark = formData.remark
    if(state.title == '新增角色'){
        params.department = formData.departmentId
    }else {
        params.id = formData.id
    }
    state.loading = true
    let method = state.title == '新增角色' ? 'post' : 'patch'
    let res = await api.changeaxiosdata('roles', method, params)
    if(res){
        ElMessage({
            message: '操作成功',
            type: 'success'
        })
        emits('cbhandle', 'role')
        dialog.value.handleClose()
    }
    state.loading = false
}

defineExpose({ openFunction })
</script>

<style lang="scss" scoped></style>